#!/usr/bin/env python
# coding: utf-8

# In[2]:


#1. Python 使用API模板


# In[4]:


import requests


# In[5]:


#1.url
base_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"

#2.api账户、通行证
API_Key = 'UmmHosUEZzJI61aqoyKPhSB8sm93xnLc'
API_Secret ='3ZTKuLwcvpALsHyFu4Z-UIL5NSIR9af_'

# 3. 请求方法：POST
# 5.准备请求参数： payload

payload = {
    "api_key":API_Key,
    "api_secret":API_Secret,
    "image_url":"https://tse1-mm.cn.bing.net/th/id/R-C.45a7b7754c4cdbd9ae34303714663c7a?rik=hIqlpUgfgLbnIQ&riu=http%3a%2f%2fwww.sk-ai.com%2fPublic%2fCrop_Big%2f30.jpg&ehk=qB9UcM9qPC0Dx0okpySDOh7InEzIG4q1DzElEoJna7s%3d&risl=&pid=ImgRaw&r=0",
    #"return_landmark":1,
    #
    
}

# 6.请求数据

r = requests.post(base_url,payload)


# In[6]:


r


# In[7]:


r.content


# In[ ]:





# In[8]:


r.json()['faces'][0]['face_token']


# In[9]:


face_token = r.json()['faces'][0]['face_token']
face_token


# In[10]:


#2.人脸分析API
使用上述模板合理调用参数实现人脸分析API
获取


# In[13]:


#1.url
analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'

#2.api账户、通行证
API_Key = 'UmmHosUEZzJI61aqoyKPhSB8sm93xnLc'
API_Secret ='3ZTKuLwcvpALsHyFu4Z-UIL5NSIR9af_'

# 3. 请求方法：POST
# 5.准备请求参数： payload

payload = {
    "api_key":API_Key,
    "api_secret":API_Secret,
    "face_tokens":face_token,
    "return_attributes":'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus'
    
}

# 6.请求数据

r = requests.post(url=analyze_url,params = payload)


# In[14]:


r


# In[15]:


#1.url
base_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"

#2.api账户、通行证
API_Key = 'UmmHosUEZzJI61aqoyKPhSB8sm93xnLc'
API_Secret ='3ZTKuLwcvpALsHyFu4Z-UIL5NSIR9af_'

# 3. 请求方法：POST
# 5.准备请求参数： payload

payload = {
    "api_key":API_Key,
    "api_secret":API_Secret,
    "image_url":"https://tse1-mm.cn.bing.net/th/id/R-C.45a7b7754c4cdbd9ae34303714663c7a?rik=hIqlpUgfgLbnIQ&riu=http%3a%2f%2fwww.sk-ai.com%2fPublic%2fCrop_Big%2f30.jpg&ehk=qB9UcM9qPC0Dx0okpySDOh7InEzIG4q1DzElEoJna7s%3d&risl=&pid=ImgRaw&r=0",
    #"return_landmark":1,
    #
    
}

# 6.请求数据

r = requests.post(base_url,payload)


# In[17]:


import pandas as pd


# # 1. 数据处理小魔法 pandas.json_normalize() 实现数据表格化

# In[18]:


pd.json_normalize(r.json()['faces']).T


# In[19]:


#1.url
base_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"

#2.api账户、通行证
API_Key = 'UmmHosUEZzJI61aqoyKPhSB8sm93xnLc'
API_Secret ='3ZTKuLwcvpALsHyFu4Z-UIL5NSIR9af_'

# 3. 请求方法：POST
# 5.准备请求参数： payload

payload = {
    "api_key":API_Key,
    "api_secret":API_Secret,
    "image_url":"https://tse1-mm.cn.bing.net/th/id/R-C.45a7b7754c4cdbd9ae34303714663c7a?rik=hIqlpUgfgLbnIQ&riu=http%3a%2f%2fwww.sk-ai.com%2fPublic%2fCrop_Big%2f30.jpg&ehk=qB9UcM9qPC0Dx0okpySDOh7InEzIG4q1DzElEoJna7s%3d&risl=&pid=ImgRaw&r=0",
    #"return_landmark":1,
    #
    
}

# 6.请求数据

r = requests.post(base_url,payload)


# In[20]:


face_token01 = r.json()["faces"][0]['face_token']
face_token01


# In[21]:


#1.url
base_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"

#2.api账户、通行证
API_Key = 'UmmHosUEZzJI61aqoyKPhSB8sm93xnLc'
API_Secret ='3ZTKuLwcvpALsHyFu4Z-UIL5NSIR9af_'

# 3. 请求方法：POST
# 5.准备请求参数： payload

payload = {
    "api_key":API_Key,
    "api_secret":API_Secret,
    "image_url":"https://gss0.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/8694a4c27d1ed21b2b25b386ab6eddc450da3fe0.jpg",
    #"return_landmark":1,
    #
    
}

# 6.请求数据

r = requests.post(base_url,payload)


# In[22]:


face_token02 = r.json()["faces"][0]['face_token']
face_token02


# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# # 3.人脸对比

# In[24]:


#1.url
compare_url = "https://api-cn.faceplusplus.com/facepp/v3/compare"

#2.api账户、通行证
API_Key = 'UmmHosUEZzJI61aqoyKPhSB8sm93xnLc'
API_Secret ='3ZTKuLwcvpALsHyFu4Z-UIL5NSIR9af_'

# 3. 请求方法：POST
# 5.准备请求参数： payload

payload = {
    "api_key":API_Key,
    "api_secret":API_Secret,
    'face_token1':face_token01,
    'face_token2':face_token02
    #
    
}

# 6.请求数据

r = requests.post(compare_url,payload)


# In[25]:


r


# In[26]:


r.json()


# In[ ]:





# # 4.封装人脸检测函数，实现快速调用

# In[30]:


API_key = 'UmmHosUEZzJI61aqoyKPhSB8sm93xnLc'
API_secret ='3ZTKuLwcvpALsHyFu4Z-UIL5NSIR9af_'
img_url_01 ="https://tse1-mm.cn.bing.net/th/id/R-C.45a7b7754c4cdbd9ae34303714663c7a?rik=hIqlpUgfgLbnIQ&riu=http%3a%2f%2fwww.sk-ai.com%2fPublic%2fCrop_Big%2f30.jpg&ehk=qB9UcM9qPC0Dx0okpySDOh7InEzIG4q1DzElEoJna7s%3d&risl=&pid=ImgRaw&r=0",
img_url_02 ="https://gss0.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/8694a4c27d1ed21b2b25b386ab6eddc450da3fe0.jpg",
   


# In[31]:


def detect_face(API_key,API_sercret,img_url):
    """face++ API人脸检测函数"""
    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
    payload = {
        'api_key':API_key,
        'api_secret':API_sercret,
        'image_url':img_url
    }
    r = requests.post(url = base_url, params = payload)
    return r.json()


# In[33]:


detect_face(API_key,API_secret,img_url_01)


# In[34]:


detect_face(API_key,API_secret,img_url_02)


# In[ ]:




